<template>
  <view class="login-container">
    <view class="input-group">
      <view class="input-item">
        <text class="input-icon">📱</text>
        <input 
          type="number" 
          placeholder="请输入手机号" 
          @input="formatPhone"
          v-model="phone"
        >
      </view>
      <view class="input-item">
        <text class="input-icon">🔒</text>
        <input 
          type="password" 
          placeholder="请输入密码" 
          v-model="password"
        >
      </view>
    </view>
    
    <button class="login-btn" @click="login">登录</button>
    <view class="link-group">
      <text @click="goToRegister">注册</text>
      <text @click="goToFindPassword">忘记密码</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      phone: '',
      password: ''
    }
  },
  methods: {
    formatPhone(e) {
      this.phone = e.detail.value.replace(/\D/g, '')
    },
    
    async login() {
      if (!this.phone) {
        return uni.showToast({ title: '请输入手机号', icon: 'none', duration: 1500 })
      }
      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        return uni.showToast({ title: '手机号格式错误', icon: 'none', duration: 1500 })
      }
      if (!this.password) {
        return uni.showToast({ title: '请输入密码', icon: 'none', duration: 1500 })
      }
      
      try {
        uni.showLoading({ title: '登录中...' })
        
        const res = await uni.$http.post('/user/login', {
          phone: this.phone,
          password: this.password
        })
        
        uni.hideLoading()
        
        if (res.statusCode !== 200) {
          throw new Error(`HTTP错误：${res.statusCode}`)
        }
        
        // login.vue 中登录成功的逻辑部分
        // login.vue 登录成功分支
        if (res.statusCode === 200 && res.data.code === 200) {
          // 关键：打印后端返回的user数据，确认有id
          console.log('登录后端返回的User数据：', res.data.data); 
          if (!res.data.data.id) {
            uni.showToast({ title: '登录异常：未获取用户ID', icon: 'none' });
            return;
          }
          // 存储用户信息（含id）
          uni.setStorageSync('userInfo', res.data.data);
          // 跳转my页面
          setTimeout(() => {
            uni.switchTab({ url: '/pages/my/my' });
          }, 1000);
        }
        else {
          uni.showToast({ 
            title: res.data.msg || '登录失败', 
            icon: 'none',
            duration: 1500
          })
        }
      } catch (err) {
        uni.hideLoading()
        console.error('登录失败：', err)
        uni.showToast({ 
          title: '网络连接失败，请稍后重试', 
          icon: 'none',
          duration: 1500
        })
      }
    },
    
    goToRegister() {
      uni.navigateTo({ url: '/my_pages/register/register' })
    },
    goToFindPassword() {
      uni.navigateTo({ url: '/my_pages/zhaohui' })
    }
  }
}
</script>

<style scoped>
/* 样式保持不变 */
.login-container {
  padding: 50rpx 30rpx;
  min-height: 100vh;
  background-color: #F8F8F8;
}
.input-group {
  margin-bottom: 60rpx;
}
.input-item {
  display: flex;
  align-items: center;
  background-color: #fff;
  padding: 0 20rpx;
  height: 90rpx;
  border-radius: 10rpx;
  margin-bottom: 20rpx;
}
.input-icon {
  width: 40rpx;
  height: 40rpx;
  margin-right: 20rpx;
}
.input-item input {
  flex: 1;
  height: 100%;
  font-size: 32rpx;
}
.login-btn {
  width: 100%;
  height: 96rpx;
  line-height: 96rpx;
  background-color: #C00000;
  color: #fff;
  font-size: 34rpx;
  border-radius: 48rpx;
  margin-bottom: 30rpx;
}
.link-group {
  display: flex;
  justify-content: space-between;
  padding: 0 20rpx;
}
.link-group text {
  font-size: 28rpx;
  color: #666;
}
</style>
